package cn.cn.tedu.file;

import java.io.*;
import java.util.Scanner;

/**
 * @author ：Dsi
 * @date ：2021/11/16 17:01
 * Describe ：该类用于IO流文件的复制
 */
public class TestCopyFile {
    public static void main(String[] args) {
        System.out.println("输入原文件的绝对地址：");
        String f = new Scanner(System.in).nextLine();
        System.out.println("输入新文件的绝对地址：");
        String f2 = new Scanner(System.in).nextLine();

        method1(f, f2);
        //method2();
    }

    private static void method1(String f, String f2) {
        BufferedInputStream bi = null;
        BufferedOutputStream bo = null;
        int a;
        try {
            bi = new BufferedInputStream(new FileInputStream(f));
            bo = new BufferedOutputStream(new FileOutputStream(f2));
            while ((a = bi.read()) != -1) {
                bo.write(a);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            /*关流有顺序，如果有多个流，先关最后创建的
              多条关流语句，需要各自try-catch
             */
            try {
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try{
                bi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
